package com.xm.adv.Ctr;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import com.xm.adv.bean.OpenCloseBean;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;

import xinya.com.baselibrary.constant.Constant;
import xinya.com.baselibrary.utils.ACacheX;
import xinya.com.baselibrary.utils.DateTransforam;
import xinya.com.baselibrary.utils.LogUtil;
import xinya.com.baselibrary.utils.ToastUtils;
import xinya.com.baselibrary.utils.gson.GenericType;
import xinya.com.baselibrary.utils.gson.GsonUtils;

/**
 * Created by zhangjiebo on 2019/6/13.
 * 自动开关机管理
 *
 * @author ZhangJieBo
 */
public class OpenCloseCtr {
    public static void init(Context context) {
        String closeOpenTime = ACacheX.getAsString(context, Constant.AcacheKey.setInfo, Constant.AcacheValue.closeOpenTime);
        if (TextUtils.isEmpty(closeOpenTime)) {
        } else {
            try {
                List<OpenCloseBean> openCloseBeans = GsonUtils.parseToGenericObject(closeOpenTime, new GenericType<List<OpenCloseBean>>() {
                });
                List<Long> endTimeList = new ArrayList<>();
                long currentTimeMillis = System.currentTimeMillis();
                int num = 0;
                for (int i = 0; i < openCloseBeans.size(); i++) {
                    if (TextUtils.equals("00:00", openCloseBeans.get(i).getStartTime()) && TextUtils.equals("00:00", openCloseBeans.get(i).getEndTime())) {
                        num++;
                    } else {
                        endTimeList.add(DateTransforam.dateToStampXXX(openCloseBeans.get(i).getEndTime()) - currentTimeMillis);
                    }
                }
                if (num == openCloseBeans.size()) {
                    LogUtil.LogShitou("OpenCloseCtr--init", "未设置开关机时间");
                    Intent intent = new Intent("android.56iq.intent.action.setpoweronoff");
                    intent.putExtra("enable", "false"); //使能开关机功能，设为false,则为关闭
                    context.sendBroadcast(intent);
                    return;
                }
                Long max = Collections.max(endTimeList);
                int near = endTimeList.indexOf(max);

                int startHour = Integer.parseInt(openCloseBeans.get(near).getStartTime().split(":")[0]);
                int startMin = Integer.parseInt(openCloseBeans.get(near).getStartTime().split(":")[1]);
                int endHour = Integer.parseInt(openCloseBeans.get(near).getEndTime().split(":")[0]);
                int endMin = Integer.parseInt(openCloseBeans.get(near).getEndTime().split(":")[1]);
                Calendar instance = Calendar.getInstance();
                int year = instance.get(Calendar.YEAR);
                int month = instance.get(Calendar.MONTH) + 1;
                int day = instance.get(Calendar.DAY_OF_MONTH);
                Intent intent = new Intent("android.56iq.intent.action.setpoweronoff");
                int[] timeonArray;
                if (startHour < endHour) {
                    timeonArray = new int[]{year, month, day + 1, startHour, startMin}; // 下 次 开 机 具 体 日 期 时 间 ， 即 在2014/10/1 8:30 会开机
                } else {
                    if (startHour == endHour && startMin < endMin) {
                        timeonArray = new int[]{year, month, day + 1, startHour, startMin};
                    } else {
                        timeonArray = new int[]{year, month, day, startHour, startMin}; // 下 次 开 机 具 体 日 期 时 间 ， 即 在2014/10/1 8:30 会开机
                    }
                }
                int[] timeoffArray = {year, month, day, endHour, endMin}; //下次关机具体日期时间， 即在 2014/9/1 8:30会关机
                intent.putExtra("timeon", timeonArray);
                intent.putExtra("timeoff", timeoffArray);
                intent.putExtra("enable", "true"); //使能开关机功能，设为false,则为关闭
                context.sendBroadcast(intent);
                ToastUtils.showToast(context, "开机时间：" + openCloseBeans.get(near).getStartTime() + "\n关机时间：" + openCloseBeans.get(near).getEndTime());
            } catch (Exception e) {
            }
        }

    }
}
